package cn.edu.abtu.service.impl;

import cn.edu.abtu.dao.EduCourseDao;
import cn.edu.abtu.dao.EduTeacherDao;
import cn.edu.abtu.entity.EduCourse;
import cn.edu.abtu.entity.EduTeacher;
import cn.edu.abtu.entity.vo.TeacherQuery;
import cn.edu.abtu.service.EduTeacherService;
import cn.edu.abtu.vo.ResponseData;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
@Slf4j
public class EduTeacherServiceImpl implements EduTeacherService {

    @Autowired
    private EduTeacherDao eduTeacherDao;

    @Autowired
    private EduCourseDao eduCourseDao;

    @Override
    public ResponseData add(EduTeacher eduTeacher) {
        eduTeacher.setGmtCreate(new Date());
        eduTeacher.setGmtModified(new Date());
        int insert = eduTeacherDao.insert(eduTeacher);
        if (insert > 0) {
            log.info("新增教师" + eduTeacher);
            return ResponseData.success();
        } else {
            return ResponseData.fail();
        }
    }

    @Override
    public ResponseData delete(String teacherId) {
        EduTeacher eduTeacher = new EduTeacher();
        eduTeacher.setId(teacherId);
        eduTeacher.setIsDeleted(1);
        int update = eduTeacherDao.updateById(eduTeacher);
        if (update > 0) {
            log.info("删除教师" + eduTeacher);
            return ResponseData.success();
        } else {
            return ResponseData.fail();
        }
    }

    @Override
    public ResponseData page(Integer pageNum, Integer pageSize, TeacherQuery teacherQuery) {
        Page pageInfo = new Page(pageNum, pageSize);
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        if (teacherQuery != null) {
            String name = teacherQuery.getName();
            Integer level = teacherQuery.getLevel();
            String begin = teacherQuery.getBegin();
            String end = teacherQuery.getEnd();
            if (!StringUtils.isEmpty(name)) {
                queryWrapper.like("name", name);
            }
            if (level != null) {
                queryWrapper.eq("level", level);
            }
            if (!StringUtils.isEmpty(begin)) {
                queryWrapper.ge("gmt_create", begin);
            }
            if (!StringUtils.isEmpty(end)) {
                queryWrapper.le("gmt_create", end);
            }
        }
        queryWrapper.orderByDesc("gmt_create");
        queryWrapper.eq("is_deleted", 0);
        Page mapsPage = eduTeacherDao.selectMapsPage(pageInfo, queryWrapper);
        return ResponseData.success("查询成功", mapsPage);
    }

    @Override
    public ResponseData getTeacherFrontInfo(String id) {
        // 1.根据讲师ID查询讲师的基本信息
        EduTeacher teacher = eduTeacherDao.selectById(id);
        if (teacher!=null){
            // 2. 根据讲师ID查询讲师讲授课程的基本信息
            QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("teacher_id",id);
            List<EduCourse> courseList = eduCourseDao.selectList(queryWrapper);
            HashMap<String,Object> hashMap = new HashMap<>();
            hashMap.put("teacher",teacher);
            hashMap.put("courseList",courseList);
            return ResponseData.success("查询成功!",hashMap);
        }
        return ResponseData.fail();
    }

    @Override
    public ResponseData getTeacherFrontList(Integer page, Integer limit) {
        Page pageInfo = new Page(page, limit);
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        Page mapsPage = eduTeacherDao.selectMapsPage(pageInfo, queryWrapper);
        List<EduTeacher> records = mapsPage.getRecords();
        long current = mapsPage.getCurrent();
        long pages = mapsPage.getPages();
        long size = mapsPage.getSize();
        long total = mapsPage.getTotal();
        boolean hasNext = mapsPage.hasNext();
        boolean hasPrevious = mapsPage.hasPrevious();
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("records",records);
        hashMap.put("current",current);
        hashMap.put("pages",pages);
        hashMap.put("size",size);
        hashMap.put("total",total);
        hashMap.put("hasNext",hasNext);
        hashMap.put("hasPrevious",hasPrevious);
        return ResponseData.success("查询成功",hashMap);
    }

    @Override
    public ResponseData queryById(String teacherId) {
        EduTeacher eduTeacher = eduTeacherDao.selectById(teacherId);
        if (eduTeacher != null) {
            return ResponseData.success("查询成功", eduTeacher);
        } else {
            return ResponseData.fail();
        }
    }

    @Override
    public ResponseData list() {
        List<EduTeacher> eduTeachers = eduTeacherDao.selectList(null);
        if (eduTeachers!=null){
            return ResponseData.success("查询成功",eduTeachers);
        }else {
            return ResponseData.fail("数据不存在",null);
        }
    }

    @Override
    public ResponseData update(EduTeacher eduTeacher) {
        eduTeacher.setGmtModified(new Date());
        int update = eduTeacherDao.updateById(eduTeacher);
        if (update > 0) {
            log.info("修改教师" + eduTeacher);
            return ResponseData.success();
        } else {
            return ResponseData.fail();
        }
    }


}
